package cn.wolfcode.rbac.web.controller;


import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
	private IPermissionService service;
	@RequestMapping("/list")
	@RequiresPermissions(value = {"权限查询","permission:list"},logical = Logical.OR)
	public String list(Model model, @ModelAttribute("qo")QueryObject qo){
		PageInfo result = service.query(qo);
		model.addAttribute("result", result);
		return "permission/list";
	}
/*	@RequestMapping("/input")
	@RequiredPermission(expression ="permission:input" ,name = "权限编辑")
	public String input(Model model,Long id){
		if(id!=null){
			Permission entity = service.selectOne(id);
			model.addAttribute("entity", entity);
		}
		return "permission/input";
	}
	@RequestMapping("/saveOrUpdate")
	@RequiredPermission(expression ="permission:saveOrUpdate" ,name = "权限更新")
	public String list(Model model,Permission u){

		service.saveOrUpdate(u);
		return "redirect:/permission/list.do";
	}*/
	@RequestMapping("/delete")
	@RequiresPermissions(value = {"权限删除","permission:delete"},logical = Logical.OR)
	public String list(Long id){
		service.delete(id);
		return "redirect:/permission/list.do";
	}
	@RequestMapping("/reload")
	@ResponseBody
	@RequiresPermissions(value = {"权限加载","permission:reload"},logical = Logical.OR)
	public JsonResult reload(){
		try {
			service.reload();
			return new JsonResult("加载完成",true);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult("加载失败",false);
		}
	}
}
